<?php

class Db_Tables2_DccLogsBalance extends Zend_Db_Table
{
    protected $_name = 'dcc_logs_balance';
	var $company_id = NULL;
	var $user_id = NULL;
	var $user_name = NULL;
	var $staff_id = NULL;
	var $staff_name = NULL;
	var $balance_type = NULL;
	var $action_type = NULL;
	var $action_value = NULL;
	var $tips = NULL;
	
	var $call_in_out = NULL;
	var $call_mins = NULL;
	
	function __construct() {
		$this->maozhua_wb = Zend_Registry::get("maozhua_wb");
	}
	
	function AddRecord()
	{
		$result = FALSE;
		
		if($this->company_id && $this->action_value && $this->action_type)
		{
			$select = $this->maozhua_wb->select();
			$select->from("dcc_companies", array("balance1", "balance2", "company_name"));
			$select->where("company_id = ?", $this->company_id);
			
			$crow = $this->maozhua_wb->fetchRow($select);
			
			if($crow)
			{
				$balance1_before = $crow['balance1']; //自充值账户
				$balance2_before = $crow['balance2']; //赠送账户
				
				$data = array(
					"company_id" => $this->company_id,
					"company_name" => $crow['company_name'],
					"user_id" => $this->user_id,
					"user_name" => $this->user_name,
					"staff_id" => $this->staff_id,
					"staff_name" => $this->staff_name,
					"action_type" => $this->action_type,
					"action_value" => $this->action_value,
					"tips" => $this->tips,
					"ctime" => date("Y-m-d H:i:s")
				);
				
				if(1 == $this->action_type && $this->balance_type) //充值
				{
					$data['balance_type'] = $this->balance_type;
					
					if(1 == $this->balance_type)
					{
						$data['balance_before'] = $balance1_before;
						$data['balance_after'] = $balance1_before + $this->action_value;
					}else{
						$data['balance_before'] = $balance2_before;
						$data['balance_after'] = $balance2_before + $this->action_value;
					}
				}elseif(2 == $this->action_type) //扣除
				{
					if($this->balance_type) //指定账户
					{
						$data['balance_type'] = $this->balance_type;

						if(1 == $this->balance_type)
						{
							$data['balance_before'] = $balance1_before;
							$data['balance_after'] = $balance1_before - $this->action_value;
						}else{
							$data['balance_before'] = $balance2_before;
							$data['balance_after'] = $balance2_before - $this->action_value;
						}
					}else{ //未指定账户，先扣赠送账户
						if(0 < $balance2_before)
						{
							$data['balance_type'] = 2;
							$data['balance_before'] = $balance2_before;
							$data['balance_after'] = $balance2_before - $this->action_value;
						}else{
							$data['balance_type'] = 1;
							$data['balance_before'] = $balance1_before;
							$data['balance_after'] = $balance1_before - $this->action_value;
						}
					}
				}
				
				$this->maozhua_wb->insert("dcc_logs_balance", $data);
				
				$set = array();
				
				if(1 == $data['balance_type'])
				{
					$set['balance1'] = $data['balance_after'];
				}elseif(2 == $data['balance_type'])
				{
					$set['balance2'] = $data['balance_after'];
				}
				
				$where = "company_id = ".$this->company_id;

				$this->maozhua_wb->update("dcc_companies", $set, $where);
				
				$result = TRUE;
			}
		}
		
		return $result;
	}
}